import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import Vant from 'vant'
import router from './router'
import api from './api/index'  // 根据你的项目路径调整
import './css/util.css'
import './css/main.css'
import './css/style.css'
import './css/banner.css'
import './css/my.css'
import './css/reset.css'
import './css/info.css'
import './css/coupon.css'
import './css/coupon-info.css'
import './css/font-awesome-4.7.0/css/font-awesome.min.css'
import 'vant/lib/index.css';
const app = createApp(App);
app.use(router);
app.use(Vant);
app.use(api);
app.mount('#app');

router.beforeEach(async (to, from, next) => {
  if (to.meta.auth) {
    // 需要登录
    // 判断 localStorage 中是否有登录信息
    const loginUserStr = localStorage.getItem('LoginUser');
    const loginUser = loginUserStr ? JSON.parse(loginUserStr) : null;

    // 没有登录就跳转到登录界面
    if (!loginUser) {
      next('/login');
      return;
    }

    // 获取用户ID
    const userid = loginUser.id || null;
    const usermobile = loginUser.mobile || null;
    const useremail = loginUser.email || null;
    // 接口请求验证
    const result = await api.check({ userid,usermobile,useremail });
    // result;
    // console.log(result, 'safdlajl');

    if (result.code === 1) {
      // localStorage.setItem('LoginUser', JSON.stringify(result.data));
      next();
    } else {
      // 验证失败，清除 localStorage
      localStorage.removeItem('LoginUser');
      next('/login');
    }

    // if (result.code === 0) {
    //   // 验证失败，清除 localStorage
    //   localStorage.removeItem('LoginUser');
    //   next('/login');
    // } else {
    //   // 验证成功，更新 localStorage
    //   localStorage.setItem('LoginUser', JSON.stringify(result.data));
    //   next();
    // }
  } else {
    // 不需要登录，直接放行
    next();
  }
});

